package com.wf.ew.orderB.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.orderB.biz.ScoreBiz;
import com.wf.ew.orderB.model.Score;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-04-15 13:27:19
 * @version ：1.0
 */
@Repository
public class ScoreBizImpl extends BaseDao implements ScoreBiz {

	@Override
	public String list(PageResult<T> page, Score score, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Score> rm = new BeanPropertyRowMapper<Score>(Score.class);
		StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.score");
        sql.append(",s.billId,s.carNo,s.createTime,s.createUser");
		sql.append(",s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_score s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(score)){
//			if(Utils.isNotEmpty(score.getName())){
//				sql.append(" and instr(s.name,:name)>0 ");
//				map.put("name", score.getName());
//			}
			if(Utils.isNotEmpty(score.getSort())&&Utils.isNotEmpty(score.getOrder())){
				sql.append(" order by "+score.getSort()+" "+score.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Score> list(Score score, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Score> rm = new BeanPropertyRowMapper<Score>(Score.class);
        StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.score");
        sql.append(",s.billId,s.carNo,s.createTime,s.createUser");
		sql.append(",s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_score s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(score)){
//			if(Utils.isNotEmpty(score.getName())){
//				sql.append(" and instr(s.name,:name)>0 ");
//				map.put("name", score.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Score getById(Score score) {
		RowMapper<Score> rm = new BeanPropertyRowMapper<Score>(Score.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.score");
        sql.append(",s.billId,s.carNo,s.createTime,s.createUser");
		sql.append(",s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_score s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 and s.id=:id ");
		map.put("id", score.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Score score, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sszg_score (");
        sql.append("companyId,score,billId,carNo");
		sql.append(",createUser) values ( ");
        sql.append(":companyId,:score,:billId,:carNo");
		sql.append(",:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("score", score.getScore());
        map.put("billId", score.getBillId());
        map.put("carNo", score.getCarNo());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Score score, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sszg_score set ");
        sql.append("score=:score,billId=:billId,carNo=:carNo ");
		sql.append(",updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("score", score.getScore());
        map.put("billId", score.getBillId());
        map.put("carNo", score.getCarNo());
		map.put("updateUser", loginUser.getId());
		map.put("id", score.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Score score, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(score.getIds().length()>0){
			for(String ID : score.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sszg_score set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
